<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['params']=array('jy'=>1);
	$return['member']=$this->pdo_get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$address=pdo_get('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['post']=array('addressid'=>$address['id'],'address'=>$address);
	set_html('title','寄卖平台');
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yangjidan_jimai',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$dj_huigouday=$this->setting['dj_huigouday'];
		foreach($list as &$item){
			if($item['endtime']<time()){
				$this->model('model')->huigou($item);
				$item['status']=3;
			}
			$item['cv_buyjd']=$this->setting['cv_buyjd']*$item['jdnum'];
			$item['endday']=date('Y-m-d',$item['endtime']);
			$item['statustitle']=$this->myji_status[$item['status']];
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='buy'){
	$postdata=get_postdata();
	$item=pdo_get('jy_yangjidan_jimai',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if(!$item['id']){
		exi('未找到该商品！','error');
	}
	if($item['status']!=1){
		exi('该商品已售空！','error');
	}
	if(!$postdata['address']['pcd']['province'] || !$postdata['address']){
		exi('请填收货地址！','error');
	}
	$item['address']=$postdata['address'];
	$member = pdo_get('jy_yangjidan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($item['price']>$member['jifen2']){
		$item['jifen2']=$member['jifen2'];
	}else{
		$item['jifen2']=$item['price'];
	}
	if(!$item['type'] && $this->setting['cv_buyjd']>0){
		$lscv=$this->model('cv')->get_lscv();
		$cv_buyjd=$this->setting['cv_buyjd']*$item['jdnum'];
		if($cv_buyjd>$member['cv']+$lscv){
			exi('可用贡献值不足，不能购买！','error');
		}
	}
	$item['price'] -=$item['jifen2'];
	$id=$this->model('model')->myjiorder($item);
	if($item['price']>0){
			$params=array(
					'id'=>$id,
					'title'=>'购买寄卖商品',
					'fee'=>$item['price'],
					'plugin'=>'jy_yangjidan',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_yangjidan/jimai/payresult/',
					'success_url'=>'jy_yangjidan/myjiorder/',
					'error_url'=>'jy_yangjidan/myjiorder/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_yangjidan_myjiorder',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('购买失败，该商品价格未设置！');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_yangjidan_myjiorder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_yangjidan_myjiorder',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	pdo_update('jy_yangjidan_jimai',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['jimaiid']));
	if($order['jifen2']>0){
		pdo_update('jy_yangjidan_member',array('jifen2 -='=>$order['jifen2']),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('支付成功','success','jy_yangjidan/myjiorder/');
}
